TGGS Computer Science

Spring 1 - Lesson 3 - Searching 2D Lists

🐍 Activity 1 – Search students by house

πŸ”Ž Understand the data

You are given a 2D list called students.

Each record contains:

  • First name [item 0]
  • House name [item 1]
  • House points [item 2]
students = [
    ["Cassie", "Robertson", 33],
    ["Emily", "Wilkinson", 31],
    ["Lydia", "Beale", 43],
    ["Nerys", "Jackson", 22],
    ["Parul", "Cross", 38],
    ["Amelia", "Robertson", 28],
    ["Sophie", "Wilkinson", 34],
    ["Hannah", "Beale", 39],
    ["Freya", "Jackson", 26],
    ["Aisha", "Cross", 35]
]

πŸ”Ž What should happen

  • The user will be asked to input a house at TGGS
  • The program will then search all records within the list for a match
  • If a match is found, the student's name is output
Enter a house at TGGS: Robertson
Students in Robertson are:
Cassie
Amelia

🐍 (1) Create the user input

Beneath the 2D list you must write a line of code where:
  • A variable is created called house
  • An input is assigned to this variable
  • The input prompts the user to "Input the name of a house at TGGS: "
Click to see how the code should look
house = input("Enter a house at TGGS: ")

🐍 (2) Output a message before displaying students in that house

Beneath the code where the user inputs a house, there should be an output.

This will confirm the house that has been selected.

e.g

Students in Robertson: 

or

Students in Cross: 
Click to see how the code should look
print(f"Students in {house}")

🐍 (3) Write the conditions for a for loop

In order to search for students in the selected house:

  • The code must contain a for loop
  • The for loop will iterate as many times as there are student records in the list
Click to see how the code should look
for i in range(0, 10):

🐍 (4) Compare the house in each record with the user input

In each iteration of the for loop, the program should check the record that currently matches i

It will then look at the house listed in this record, to see if it is a match for the house the user entered

Click to see how the code should look
for i in range(0, 10):
    if students[i][1] == house:

🐍 (5) Output the name of each student in the house

If the record contains the name of the house the user has searched for...

The name of the student will be output.

Click to see how the code should look
for i in range(0, 10):
    if students[i][1] == house:
        print(students[i][0])
🐍 Activity 2 – Find a landmark’s country

πŸ”Ž Analyse the data

Each record contains:

  • The name of a famous landmark [item 0]
  • The country in which it is found [item 1]
sites = [
    ["Mount Everest", "Nepal"],
    ["Grand Canyon", "United States"],
    ["Kilamanjaro", "Tanzania"],
    ["Mount Fuji", "Japan"],
    ["Uluru", "Australia"],
    ["Mount Etna", "Italy"],
    ["Wistmans Wood", "UK"]
]

πŸ”Ž What should happen

  • The user will be prompted to input the name of a landmark
  • The program will then search all records in the list for a match
  • If a match is found, the program will output the name of the country
Enter a landmark: Uluru
Uluru is found in Australia

🐍 (1) Create a user input - for name of landmark

  • The user will be prompted to input the name of a landmark
  • This will be assigned to a variable called lm
Click to see how the code should look
lm = input("Enter the name of a famous landmark: ")

🐍 (2) Write the conditions for a for loop

In order to search for the country of the landmark entered:

  • The code must use a for loop
  • The for loop will iterate as many times as there are records in the list
Click to see how the code should look
for i in range(0, 7):

🐍 (3) Use selection (IF / ELSE) to find a match

Use an IF / ELSE statement to identify if the user input is a match for the landmark in each record

Click to see how the code should look
for i in range(0, 7):
    if lm == sites[i][0]:

🐍 (4) Output the name of the country for a match

Output the landmark searched for and the name of its country

Uluru is found in australia
Click to see how the code should look
for i in range(0, 7):
    if lm == sites[i][0]:
        print(f"{lm} is found in {sites[i][1]}")
🐍 Activity 3 - Top scoring house points

πŸ”Ž Understand the data

Again you are given a 2D list called students.

Each record contains:

  • First name [item 0]
  • House name [item 1]
  • House points [item 2]
students = [
    ["Cassie", "Robertson", 33],
    ["Emily", "Wilkinson", 31],
    ["Lydia", "Beale", 43],
    ["Nerys", "Jackson", 22],
    ["Parul", "Cross", 38],
    ["Amelia", "Robertson", 28],
    ["Sophie", "Wilkinson", 34],
    ["Hannah", "Beale", 39],
    ["Freya", "Jackson", 26],
    ["Aisha", "Cross", 35]
]

πŸ”Ž What should happen

  • The user will be prompted to input a minimum house point score
  • The program will then search the list for records with this score or above
  • The program will then output the name of each matching student and their score
Enter a minimum number of house points: 38
Students with 38 or more house points:
Lydia - 43
Parul - 38
Hannah - 39

🐍 (1) Create a user input

  • The user will be prompted to input a minimum number of house points
  • As the input will be a number, it must be cast to integer
  • This will be assigned to a variable called hp
Click to see how the code should look
hp = int(input("Enter a minimum number of house points: "))

🐍 (2) Confirm the selection

  • The program will output a message
  • It will confirm the minimum number of house points selected
Students with 38 or more house points:
Click to see how the code should look
print(f"Students with {hp} or more house points:")

🐍 (3) Create conditions for a for loop

  • A for loop is created
  • It will use a range from 0 to the length of the students list
  • See below for details
Click to see how the code should look

By using the len() function we don't need to know how long the list is

Python will work this out for us

for i in range(0, len(students)):

🐍 (4) Records with house points greater than or equal to the input number

Use selection (IF / ELSE) to check if the number of house points is greater than or equal to the input

Click to see how the code should look
for i in range(0, len(students)):
    if students[i][2] >= hp:

🐍 (5) Output the names of students who match the criteria

Use an f string to output the names of any students who match the criteria

Click to see how the code should look
for i in range(0, len(students)):
    if students[i][2] >= hp:
        print(f"{students[i][0]} - {students[i][2]} house points.")
🐍 Activity 4 - Your Own Search

πŸ”Ž Understand the task

  • Create a 2D list based on a topic of your choice
  • Enable the user to search through the records of your 2D list